using System;
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;
using StrategicDataSystems.Cloud.Monitoring.Configuration;
using StrategicDataSystems.Cloud.Monitoring.HostedServices;
using StrategicDataSystems.Cloud.Monitoring.UI.Services;
using StrategicDataSystems.Cloud.Monitoring.UI.ViewModels;
using StrategicDataSystems.Cloud.Monitoring.UI;

namespace StrategicDataSystems.Cloud.Monitoring.Tests.Given_A_SysTrayViewModel
{
    [TestFixture]
    public class When_A_Deployment_Is_Found
    {
        [Test]
        public void Should_Set_SubscriptionHasCurrentDeployment_Flag_To_True()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var stubTimerService = MockRepository.GenerateMock<ITimerService>();

            var fakeHostedServices = new List<HostedService>() { 
                    new HostedService("service1", new Uri("http://management.net"), 
                        new List<Deployment> () { new Deployment("Production", new Uri("http://exmaple.com"), "v1.0", "Running", "test") } ) };

            Settings settings = new Settings();

            stubHostedServiceGateway.Stub(
                x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint))
                .Return(fakeHostedServices);
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(true);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, stubNotificationService, stubTimerService, stubNetPresenceService);

            Assert.IsTrue(viewModel.SubscriptionHasCurrentDeployments, "Should have indicated a deployment.");
            
        }

        [Test]
        public void Should_Expose_Active_Deployments()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var stubTimerService = MockRepository.GenerateMock<ITimerService>();

            var testDeploymentsForService1 = new Deployment("Production", 
                                                            new Uri("http://exmaple.com/1"), 
                                                            "v1.0", 
                                                            "Running", 
                                                            "test");

            var testDeploymentsForService2 = new Deployment("Staging", 
                                                            new Uri("http://exmaple.com/2"), 
                                                            "v1.1", 
                                                            "Suspended", 
                                                            "test");

            var fakeHostedServices = new List<HostedService>() { 
                    new HostedService("service1", new Uri("http://management.net"), new List<Deployment>() {testDeploymentsForService1 } ),
                    new HostedService("service2", new Uri("http://management2.net"), new List<Deployment>() {testDeploymentsForService2 } )};

            Settings settings = new Settings();

            stubHostedServiceGateway.Stub(
                x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint))
                .Return(fakeHostedServices);
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(true);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, stubNotificationService, stubTimerService, stubNetPresenceService);

            CollectionAssert.Contains(viewModel.ActiveDeployments, testDeploymentsForService1);
            CollectionAssert.Contains(viewModel.ActiveDeployments, testDeploymentsForService2);
        }

        [Test]
        public void Should_Notify_User_Of_Deployments()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var mockNotificationService = MockRepository.GenerateMock<INotificationService>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var stubTimerService = MockRepository.GenerateMock<ITimerService>();

            var fakeHostedServices = new List<HostedService>() { 
                    new HostedService("service1", new Uri("http://management.net"), 
                        new List<Deployment> () { new Deployment("Production", new Uri("http://exmaple.com"), "v1.0", "Running", "Test") } ) };

            Settings settings = new Settings();

            stubHostedServiceGateway.Stub(
                x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint))
                .Return(fakeHostedServices);
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(true);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, mockNotificationService, stubTimerService, stubNetPresenceService);

            mockNotificationService.AssertWasCalled(x => x.NotifyUserOfActiveDeployment(1));
        }


    }
}
